package com.meitianhui.convenitenbuy.module.store.presenter;

import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseArrayResponse;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.main.http.MainApiDataSource;
import com.meitianhui.convenitenbuy.main.model.bean.AppToken;
import com.meitianhui.convenitenbuy.module.store.contract.StoreContract;
import com.meitianhui.convenitenbuy.module.store.http.StoreApiDataSource;
import com.meitianhui.convenitenbuy.module.store.model.StoreInfo;
import com.meitianhui.convenitenbuy.module.store.model.StoreType;
import com.meitianhui.convenitenbuy.module.store.ui.StoreFragment;
import com.meitianhui.convenitenbuy.widget.StorePopWindow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2017/11/2.
 */

public class StorePresenter implements StoreContract.Presenter {

    private StoreFragment context;
    private final StoreContract.View mView;

    public StorePresenter(StoreContract.View view, StoreFragment context) {
        this.mView = view;
        this.context = context;
    }

    @Override
    public void getAppToken() {
        MainApiDataSource.getInstance()
                .getAppToken(Constants.APP_ID, Constants.PRIVATE_KEY)
                .subscribe(new ResponseObserver<BaseResponse<AppToken>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<AppToken> response) {
                        mView.getAppTokenSuccess(response.data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.onFail(code, msg);
                    }
                });
    }

    @Override
    public void getStoreList(String longitude, String latitude, String range,
                             String storeType, String pageIndex, String pageSize) {
        Map<String, String> map = new HashMap<>();
        map.put("longitude", longitude + "");
        map.put("latitude", latitude + "");
        if (!"30000".equals(range)) {
            map.put("range", range);
        }
        if (!TextUtils.isEmpty(storeType)) {
            map.put("stores_type_group", storeType);
        }
        Map<String, String> maps = new HashMap<>();
        maps.put("page_no", pageIndex + "");
        maps.put("page_size", pageSize + "");
        String pageJson = new Gson().toJson(maps);
        String paramJson = new Gson().toJson(map);

        StoreApiDataSource.getInstance()
                .getStoreList(InfoProvider.getAppToken(App.getInstance()),
                        "stores.consumer.nearbyStoresTypeListPageFind", pageJson, paramJson)
                .subscribe(new ResponseObserver<BaseResponse<StoreInfo>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<StoreInfo> response) {
                        mView.getStoreListSuccess(response.data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.getStoreListFail(code, msg);
                    }
                });
    }

    @Override
    public void getStoreType() {
        Map<String, String> params = new HashMap<>();
        String paramJson = new Gson().toJson(params);

        StoreApiDataSource.getInstance()
                .getStoreType(InfoProvider.getAppToken(App.getInstance()),
                        "stores.consumer.storesTypeGroupFind", paramJson)
                .subscribe(new ResponseObserver<BaseArrayResponse<StoreType>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseArrayResponse<StoreType> response) {
                        mView.getStoreTypeSuccess(response.data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.onFail(code, msg);
                    }
                });
    }

    @Override
    public void setOnDistance() {
        List<StoreType> mStoreTypeList = new ArrayList<>();
        // 地区
        StoreType storeType = new StoreType();
        storeType.setIndex("500");
        storeType.setContent("<500m");
        mStoreTypeList.add(storeType);
        StoreType storeType1 = new StoreType();
        storeType1.setIndex("1000");
        storeType1.setContent("<1km");
        mStoreTypeList.add(storeType1);
        StoreType storeType2 = new StoreType();
        storeType2.setIndex("3000");
        storeType2.setContent("<3km");
        mStoreTypeList.add(storeType2);
        StoreType storeType3 = new StoreType();
        storeType3.setIndex("30000");
        storeType3.setContent("全城");
        mStoreTypeList.add(storeType3);
        View view = context.getActivity().findViewById(R.id.ll_store);
        new StorePopWindow(context.getActivity(), view, mStoreTypeList, new StorePopWindow.OnItemClickListener() {
            @Override
            public void onClick(String id, String content) {
                mView.getDistance(id, content);
            }
        });
    }

    @Override
    public void setOnType(List<StoreType> data) {
        View view = context.getActivity().findViewById(R.id.ll_store);
        new StorePopWindow(context.getActivity(), view, data, new StorePopWindow.OnItemClickListener() {
            @Override
            public void onClick(String id, String content) {
                mView.getType(id, content);
            }
        });
    }

    @Override
    public void setOnRecommend() {
        List<StoreType> mStoreTypeList = new ArrayList<>();
        StoreType storeType = new StoreType();
        storeType.setIndex("1");
        storeType.setContent("距离优先");
        mStoreTypeList.add(storeType);
        StoreType storeType1 = new StoreType();
        storeType1.setIndex("2");
        storeType1.setContent("最新发布");
        mStoreTypeList.add(storeType1);
        StoreType storeType2 = new StoreType();
        storeType2.setIndex("3");
        storeType2.setContent("销量优先");
        mStoreTypeList.add(storeType2);
        View view = context.getActivity().findViewById(R.id.ll_store);
        new StorePopWindow(context.getActivity(), view, mStoreTypeList, new StorePopWindow.OnItemClickListener() {
            @Override
            public void onClick(String id, String content) {
                mView.getRecommend(id, content);
            }
        });
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

}
